package com.psa.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.psa.element.*;
import com.psa.system.*;
import com.wxw.BaseLibrary.drawable.PanelStyle;

import java.util.List;

/**
 * Created by david on 2014/5/13.
 */
public class SqlObjectListAdapter extends BaseAdapter {
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private List<ISqlObject> iSqlObjects = null;
    private int index = -1;

    public SqlObjectListAdapter(Context context, List<ISqlObject> data){
        _context = context;
        _inflater = LayoutInflater.from(_context);
        iSqlObjects = data;
    }
    @Override
    public int getCount() {
        return iSqlObjects==null?0:iSqlObjects.size();
    }

    @Override
    public Object getItem(int i) {
        return iSqlObjects==null || i<0 || i>=iSqlObjects.size()?null:iSqlObjects.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        HelperClass helperClass = null;
        ISqlObject sqlObject = iSqlObjects.get(i);
        if (convertView == null){
            convertView = _inflater.inflate(R.layout.sqlobj_item_view, null);
            helperClass = new HelperClass();
            helperClass.ItemText = (TextView)convertView.findViewById(R.id.ItemText);
            convertView.setTag(helperClass);
        } else {
            helperClass = (HelperClass)convertView.getTag();
        }
        if (i == index)
            convertView.setBackground(PanelStyle.createSkyBluePanelStyle());
        else
            convertView.setBackgroundColor(Color.WHITE);
        helperClass.ItemText.setText(sqlObject.toString());
        return convertView;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    class HelperClass{
        public TextView ItemText;
    }
}
